package cn.edu.scut.sys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.scut.sys.service.TrainItemService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.TrainItem;


@Controller
@RequestMapping("/trainItem")
public class TrainItemController {

	private TrainItemService trainItemService;

	@Resource(name="trainItemService")
	public void setTrainItemService(TrainItemService trainItemService) {
		this.trainItemService = trainItemService;
	}
	
	/**
	 * 
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 * @param search
	 * @param searchField
	 * @param searchOper
	 * @param searchString
	 * @param filters
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public DataResponse<TrainItem> list(@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters  
            ){  
		 try {  
	            DataRequest request = new DataRequest();  
	            request.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            request.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            request.setSidx(sidx);  
	            request.setSord(sord);  
	            request.setSearch(search);  
	            request.setSearchField(searchField);  
	            request.setSearchOper(searchOper);  
	            request.setSearchString(searchString);  
	            return trainItemService.list(request);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}
	
	@RequestMapping("/add")
	public String add(String description, int threshold,int compare,HttpServletRequest request){
		
		try{
			// 训练项目增加
			TrainItem trainItem = new TrainItem();
			if (null != description)
			trainItem.setDescription(description);
			trainItem.setThreshold(threshold);
			trainItem.setCompare(compare);
			trainItemService.add(trainItem);
			request.setAttribute("status", 1);
		}catch(Exception ex){
			request.setAttribute("status", 0);
		}
		return "/page/trainItemAdd";
	}
	
	
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable(value = "id") Long id,HttpServletRequest request){
		if (null == id) {
			return "redirect:/page/trainItemList.jsp";
		}
		
		TrainItem trainItem=trainItemService.getTrainItem(id);
		
		if (null == trainItem) {
			return "redirect:/page/trainItemList.jsp";
		}
		
		request.setAttribute ("trainItem", trainItem);
		
		return "page/trainItemEdit";
	}
	
	@RequestMapping("/update")
	public String update(TrainItem trainItem,HttpServletRequest request){
		try{
		if (null == trainItem) {
			System.out.println("trainItem");
			request.setAttribute("status", 0);
			return "/page/trainItemEdit";
		}

		
		if (null == trainItem.getThreshold()) {
			System.out.println("trainItempassword");
			request.setAttribute("status", 0);
			return "/page/trainItemEdit";
		}
		
		if (null == trainItem.getCompare()) {
			System.out.println("trainItem");
			request.setAttribute("status", 0);
			return "/page/trainItemEdit";
		}
		trainItemService.updateTrainItem(trainItem);
		request.setAttribute("status", 1);
		return "/page/trainItemEdit";
		}
		catch(Exception e){
			request.setAttribute("status", 0);
			return "/page/trainItemEdit";
		}
		
	}
	
	
	@RequestMapping("/del/{id}")
	public String del(@PathVariable(value = "id") int id,HttpServletRequest request){
		trainItemService.delTrainItem(id);
	return "redirect:/page/trainItemList.jsp";
	}
}
